export default {
  namespaced: true,

  state: {
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    // 登录成功后的 token 字符串，无论如何都得不到就伪造一个
    token:uni.getStorageSync('token') || '',
    userinfo:JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    redirectInfo:null,
  },

  mutations: {
    // 更新重定向的信息对象
    updateRedirectInfo(state,redirectInfo ) {
      state.redirectInfo = redirectInfo;
      this.commit('m_user/saveRedirectInfoToStorage');
    },
    // 持久化存储到本地
    saveRedirectInfoToStorage(state) {
      uni.setStorageSync('redirectInfo', JSON.stringify(state.redirectInfo))
    },
    // 更新地址
    updateAddress(state, address) {
      state.address = address;
      this.commit('m_user/saveAddressToStorage');
    },
    // 持久化存储到本地
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 更新用户信息
    updateUserInfo(state,userinfo){
      state.userinfo = userinfo;
      this.commit('m_user/saveUserInfoToStorage')
    },
    // 将用户信息持久化存储到本地
    saveUserInfoToStorage(state){
      uni.setStorageSync('userinfo',JSON.stringify(state.userinfo))
    },
     // 更新 token 字符串
    updateToken(state,token){
      state.token = token;
      this.commit('m_user/saveTokenToStorage')
    },
    // 将用户信息持久化存储到本地
    saveTokenToStorage(state){
      uni.setStorageSync('token',state.token)
    },
  },

  getters: {
    addStr(state) {
      if (!state.address.provinceName) return ''
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}
